package com.zhangwei.yueshijia.view.activity;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.widget.Toast;

import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.OnTabSelectListener;
import com.zhangwei.yueshijia.R;
import com.zhangwei.yueshijia.base.BaseActivity;
import com.zhangwei.yueshijia.base.BaseApp;
import com.zhangwei.yueshijia.utils.AppManager;
import com.zhangwei.yueshijia.utils.FragNavController;
import com.zhangwei.yueshijia.view.fragment.HomeFragment;
import com.zhangwei.yueshijia.view.fragment.MyFragment;
import com.zhangwei.yueshijia.view.fragment.ShopFragment;
import com.zhangwei.yueshijia.view.fragment.SpecialFragment;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * 声明
 * 初始化View
 *
 * okhttp  + Retrofit + 注解形式请求
 * 加载数据
 * json    Gson      fastjson
 *
 * 给控件赋值
 * 内存泄露
 *
 * BottomBar   FragNavController
 */

public class MainActivity extends BaseActivity implements OnTabSelectListener {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.bottom_bar)
    BottomBar bottomBar;
    FragNavController controller;

    @Override
    public void initBefore(Bundle savedInstanceState) {
        super.initBefore(savedInstanceState);
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(HomeFragment.newInstance());
        fragments.add(new SpecialFragment());
        fragments.add(new ShopFragment());
        fragments.add(new MyFragment());
        controller = new FragNavController(
                savedInstanceState
                , getSupportFragmentManager()
                , R.id.fragment_container
                , fragments
                , FragNavController.TAB3);
    }

    @Override
    protected void initView() {
        setSupportActionBar(toolbar);
        bottomBar.setOnTabSelectListener(this);
    }

    @Override
    protected void initData() {

    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void onTabSelected(@IdRes int tabId) {
        switch (tabId) {
            case R.id.tab_home:
                controller.switchTab(FragNavController.TAB1);
                break;
            case R.id.tab_special:
                controller.switchTab(FragNavController.TAB2);
                break;
            case R.id.tab_shop:
                controller.switchTab(FragNavController.TAB3);
                break;
            case R.id.tab_my:
                controller.switchTab(FragNavController.TAB4);
                break;
        }
    }

    private long firstTime = 0;

    @Override
    public void onBackPressed() {
        long secondTime = System.currentTimeMillis();
        if (secondTime - firstTime > 2000) {
            Toast.makeText(MainActivity.this, "再按一次退出程序--->onKeyUp", Toast.LENGTH_SHORT).show();
            firstTime = secondTime;
        } else {
            System.exit(0);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BaseApp.getInstance().unRegisterNetworkStateReceiver();
        AppManager.getAppManager().AppExit(BaseApp.getInstance());
    }


}
